package com.EquationSolver;

import com.EquationSolver.SquareEquation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GraphPanel extends JPanel {
	public GraphPanel() {
	}
	
	private SquareEquation formula;
	
	void setFormula(SquareEquation formula)
	{
		this.formula = formula;
	}
	
	void paintFormula(Graphics g)
	{
		g.setColor(Color.RED);
		int width = getWidth();
		int height = getHeight();
		
		if (formula != null)
		{
			for(int i = 0; i<width;i++)
			{
				g.drawLine(i,height-(int)formula.valueAt(i),i-1,height-(int)formula.valueAt(i-1));
			}
		}
		else
		{
			Dimension d = getPreferredSize();

		    g.setFont(new Font("TimesRoman", Font.PLAIN, 20));
		     
		    g.setColor(Color.red);
			g.drawString("No formula defined", d.width/2, d.height/2);
		}
	}
	
	public void paintComponent(Graphics g) {
	    int width = getWidth();
	    int height = getHeight();
	    
	    g.setColor(Color.WHITE);
	    g.fillRect(0, 0, width - 1, height -1);
	    g.setColor(Color.BLUE);
	    g.drawRect(0, 0, width - 1, height -1);
	    
	    paintFormula(g);
	  }
}
